package controllers;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

import models.SolutionAdoption;
import models.SolutionSubmission;


public class AdoptionConnect {
	boolean hasEntries;
	private int iROWS;
	
	public AdoptionConnect(){
		hasEntries = false;
		iROWS = -1;
	}
	public boolean hasEntries(){
		return hasEntries;
	}
	private HashMap<String,Object> populateValues(SolutionAdoption f)
	{
	
		HashMap<String,Object> itemMap = new HashMap<String,Object>();
		
		itemMap.put("adoptID", f.getAdoptID());
		itemMap.put("adoptCode", f.getAdoptCode());
		itemMap.put("solID", f.getSolID());
		itemMap.put("solCode", f.getSolCode());
		itemMap.put("solTitle", f.getSolTitle());
		itemMap.put("dtImplemented", f.getDtImplemented());
		itemMap.put("adptTeamLead", f.getAdptTeamLead());
		itemMap.put("adptMembers", f.getAdptMembers());
		itemMap.put("adptOthersMembers", f.getAdptOthersMembers());
		itemMap.put("Customer", f.getCustomer());    				
		itemMap.put("Location", f.getLocation());
		itemMap.put("LocationCode", f.getLocationCode());
		itemMap.put("LocationRegionName", f.getLocationRegionName());
		itemMap.put("processArea", f.getProcessArea());
		itemMap.put("adptTeamPicKey", f.getAdptTeamPicKey());
		itemMap.put("easyRank", f.getEasyRank());
		itemMap.put("easyRankDesc", f.getEasyRankDesc());
		itemMap.put("achievedType", f.getAchievedType());
		itemMap.put("achievedTypeDesc", f.getAchievedTypeDesc());
		itemMap.put("achievedValue", f.getAchievedValue());
		itemMap.put("achievedUnits", f.getAchievedUnits());
		itemMap.put("hardSavings", f.getHardSavings());
		itemMap.put("costAvoidance", f.getCostAvoidance());
		itemMap.put("oppurtunityCost", f.getOppurtunityCost());
		itemMap.put("hasHardSavings", f.isHasHardSavings());
		itemMap.put("hasCostAvoidance", f.isHasCostAvoidance());
		itemMap.put("hasOppurtunityCost", f.isHasOppurtunityCost());		
		itemMap.put("annProdSavings", f.getAnnProdSavings());
		itemMap.put("impDays", f.getImpDays());
		itemMap.put("attachKey", f.getAttachKey());
		itemMap.put("Comment", f.getComment());  
		itemMap.put("sYear", f.getsYear());
		itemMap.put("sMonth", f.getsMonth());
		
		itemMap.put("submittedBY", f.getSubmittedBY());
		itemMap.put("submittedON", f.getSubmittedON());
		itemMap.put("modifiedBY", f.getModifiedBY());
		itemMap.put("modifiedON", f.getModifiedON());
		
		return itemMap;		
		
	}
	
	public List<HashMap<String,Object>> getCompleteData(String... fieldNames) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = -1;
		
    	try {
    		Query<SolutionAdoption> q = ofy.query(SolutionAdoption.class);             
    		for ( SolutionAdoption f : q.fetch() ) {
    			HashMap<String,Object> itemMap = new HashMap<String,Object>();
    			for ( String fields : fieldNames) 
    			{
    				if ( fields.equalsIgnoreCase("adoptID") )   		{   itemMap.put("adoptID", f.getAdoptID()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("adoptCode") )   		{   itemMap.put("adoptCode", f.getAdoptCode()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("solID") )   			{   itemMap.put("solID", f.getSolID()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("solCode") )   			{   itemMap.put("solCode", f.getSolCode()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("solTitle") )   		{   itemMap.put("solTitle", f.getSolTitle()); hasEntries = true; }    				
    				if ( fields.equalsIgnoreCase("dtImplemented") ) 	{ 	itemMap.put("dtImplemented", f.getDtImplemented()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("adptTeamLead") ) 		{ 	itemMap.put("adptTeamLead", f.getAdptTeamLead()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("adptMembers") )  		{ 	itemMap.put("adptMembers", f.getAdptMembers());  hasEntries = true; }
    				if ( fields.equalsIgnoreCase("adptOthersMembers") )	{ 	itemMap.put("adptOthersMembers", f.getAdptOthersMembers());  hasEntries = true; }
    				if ( fields.equalsIgnoreCase("Customer") )  		{ 	itemMap.put("Customer", f.getCustomer()); hasEntries = true; }    				
    				if ( fields.equalsIgnoreCase("Location") ) 			{	itemMap.put("Location", f.getLocation()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("LocationCode") )		{	itemMap.put("LocationCode", f.getLocationCode()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("LocationRegionName") ){	itemMap.put("LocationRegionName", f.getLocationRegionName()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("processArea") )		{	itemMap.put("processArea", f.getProcessArea()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("adptTeamPicKey") )	{	itemMap.put("adptTeamPicKey", f.getAdptTeamPicKey()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("easyRank") )			{	itemMap.put("easyRank", f.getEasyRank()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("easyRankDesc") )		{	itemMap.put("easyRankDesc", f.getEasyRankDesc()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("achievedType") )		{	itemMap.put("achievedType", f.getAchievedType()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("achievedTypeDesc") )	{	itemMap.put("achievedTypeDesc", f.getAchievedTypeDesc()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("achievedValue") )		{	itemMap.put("achievedValue", f.getAchievedValue()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("achievedUnits") )		{	itemMap.put("achievedUnits", f.getAchievedUnits()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("hardSavings") )		{	itemMap.put("hardSavings", f.getHardSavings()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("costAvoidance") )		{	itemMap.put("costAvoidance", f.getCostAvoidance()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("oppurtunityCost") )	{	itemMap.put("oppurtunityCost", f.getOppurtunityCost()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("hasHardSavings") )	{	itemMap.put("hasHardSavings", f.isHasHardSavings()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("hasCostAvoidance") )	{	itemMap.put("hasCostAvoidance", f.isHasCostAvoidance()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("hasOppurtunityCost") ){	itemMap.put("hasOppurtunityCost", f.isHasOppurtunityCost()); hasEntries = true; }    				
    				if ( fields.equalsIgnoreCase("annProdSavings") )	{	itemMap.put("annProdSavings", f.getAnnProdSavings()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("impDays") )			{	itemMap.put("impDays", f.getImpDays()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("attachKey") )			{	itemMap.put("attachKey", f.getAttachKey()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("Comment") )			{	itemMap.put("Comment", f.getComment()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("sYear") )				{   itemMap.put("sYear", f.getsYear()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("sMonth") )			{   itemMap.put("sMonth", f.getsMonth()); hasEntries = true; }
    				
    				if ( fields.equalsIgnoreCase("submittedBY") )		{   itemMap.put("submittedBY", f.getSubmittedBY()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("submittedON") )		{	itemMap.put("submittedON", f.getSubmittedON()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("modifiedBY") )		{   itemMap.put("modifiedBY", f.getModifiedBY()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("modifiedON") )		{	itemMap.put("modifiedON", f.getModifiedON()); hasEntries = true; }
    				
    			}    			
				iROWS++;    			
    			objList.add(itemMap);
    		}
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}
	
	public List<HashMap<String,Object>> getCompleteData() {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = -1;
		
    	try {
    		Query<SolutionAdoption> q = ofy.query(SolutionAdoption.class);
 
    		for ( SolutionAdoption f : q.fetch() ) {
    			
    				HashMap<String,Object> itemMap = populateValues(f);
    				
    				hasEntries = true;
    				iROWS++;
    				
    				objList.add(itemMap);
    		}
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}

	public List<HashMap<String,Object>> getQuery(String fieldName, String Value) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = -1;
		
    	try {
    		Query<SolutionAdoption> q = ofy.query(SolutionAdoption.class).filter(fieldName, Value);
 
    		for ( SolutionAdoption f : q.fetch() ) {
    			
    				HashMap<String,Object> itemMap = populateValues(f);        			
    				
    				hasEntries = true;
    				iROWS++;
    				
    				objList.add(itemMap);
    		}
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}

	public List<HashMap<String,Object>> getQuery(String fieldName, Date Value) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = -1;
		
    	try {
    		Query<SolutionAdoption> q = ofy.query(SolutionAdoption.class).filter(fieldName, Value);
 
    		for ( SolutionAdoption f : q.fetch() ) {
    			
    				HashMap<String,Object> itemMap = populateValues(f);        			
    				
    				hasEntries = true;
    				iROWS++;
    				
    				objList.add(itemMap);
    		}
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}
	
	public List<HashMap<String,Object>> getQueryShared(String fieldName, String Value) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = -1;
		
    	try {
    		Query<SolutionAdoption> q = ofy.query(SolutionAdoption.class).filter(fieldName, Value);
 
    		for ( SolutionAdoption f : q.fetch() ) {
    			
    				HashMap<String,Object> itemMap = populateValues(f);        			
    				
    				hasEntries = true;
    				iROWS++;
    				
    				objList.add(itemMap);
    		}
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}
	
	public List<HashMap<String,Object>> getQuery(String fieldName, long Value) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = -1;
		
    	try {
    		
    		Query<SolutionAdoption> q = ofy.query(SolutionAdoption.class).filter(fieldName, Value);
 
    		for ( SolutionAdoption f : q.fetch() ) {
    			
    				HashMap<String,Object> itemMap = populateValues(f);
    				
    				hasEntries = true;
    				iROWS++;
    				
    				objList.add(itemMap);
    		}
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}
	
	public int getRows(){
		return iROWS;
	}
	
}